from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# 1.导入Select类
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('file:///D:/nsd2404/html/register.html')
# 定位下拉框，切换选项
# sleep(3)
# driver.find_element(By.CSS_SELECTOR, '[value="sh"]').click()
# sleep(3)
# driver.find_element(By.CSS_SELECTOR, '[value="gz"]').click()
# 定位下拉框元素
el = driver.find_element(By.CSS_SELECTOR, '#select')
# 2.实例化Select类
select = Select(el)
# 3.1 通过下标切换选项  从0开始
# sleep(3)
# select.select_by_index(1)  # 切换到索引为1的选项
# sleep(3)
# select.select_by_index(2)
# 3.2 通过value值切换选项
# sleep(3)
# select.select_by_value('sh')
# sleep(3)
# select.select_by_value('cq')
# 3.3 通过文本值切换选项
sleep(3)
select.select_by_visible_text('上海')
sleep(3)
select.select_by_visible_text('重庆')

sleep(3)
driver.quit()